﻿using System;
using DataAccess.Repository;

namespace KPI
{
	public partial class ProgressDetail : BasePage
	{
		private IKPIRepository _KPIRepository;
		private IKPIProgressRepository _KPIProgressRepository;

		private DataAccess.Models.KPI _TheKPI;

		public int TheKPIID
		{
			get
			{
				return CommonHelper.QueryStringInt("kpiid");
			}
		}

		public DataAccess.Models.KPI TheKPI
		{
			get
			{
				if(_TheKPI == null && TheKPIID > 0)
				{
					if(_KPIRepository == null)
					{
						_KPIRepository = new KPIRepository();
					}

					_TheKPI = _KPIRepository.GetTheKPI(TheKPIID);
				}

				return _TheKPI;
			}
		}

		protected void Page_Init(object sender, EventArgs e)
		{
			if(!CheckPermission())
			{
				Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
			}
			else
			{
				btnAgree.Visible = true;
				btnNotAgree.Visible = true;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindData();
			}
		}

		protected void btnReturn_Click(object sender, EventArgs e)
		{
			Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
		}

		protected void btnNotAgree_Click(object sender, EventArgs e)
		{
			if(TheKPI != null)
			{
				if(_KPIProgressRepository == null)
				{
					_KPIProgressRepository = new KPIProgressRepository();
				}

				if(_KPIProgressRepository.DeleteKPIProgressForUpdate(TheKPIID))
				{
					TheKPI.PreState = TheKPI.CurrentState;
					TheKPI.CurrentState = DataAccess.Models.KPIStates.Progress_Of_Modification_Modified_Review_Not_Approved;

					if(_KPIRepository.UpdateKPI(TheKPI, false))
					{
						AddLatestInfo("KPI管理平台消息：您有一条KPI指标进度修改的已审核,但没有通过。", TheKPI);

						Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
					}
					else
					{
						ShowMessage("出错了,请稍后再试或联系相关技术人员.", true);
					}
				}
				else
				{
					ShowMessage("出错了,请稍后再试或联系相关技术人员.", true);
				}
			}
		}

		protected void btnAgree_Click(object sender, EventArgs e)
		{
			if(TheKPI != null)
			{
				if(_KPIProgressRepository == null)
				{
					_KPIProgressRepository = new KPIProgressRepository();
				}

				if(_KPIProgressRepository.SaveKPIProgress(TheKPI.TheKPIProgress, true))
				{
					TheKPI.PreState = TheKPI.CurrentState;
					TheKPI.CurrentState = DataAccess.Models.KPIStates.Progress_Of_Modification_Modified_Review_Approved;

					if(_KPIRepository.UpdateKPI(TheKPI, false))
					{
						AddLatestInfo("KPI管理平台消息：您有一条KPI指标进度修改的已审核通过。", TheKPI);

						Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
					}
					else
					{
						ShowMessage("出错了,请稍后再试或联系相关技术人员.", true);
					}
				}
				else
				{
					ShowMessage("出错了,请稍后再试或联系相关技术人员.", true);
				}
			}
		}

		protected void Page_UnLoad(object sender, EventArgs e)
		{
			if(_KPIRepository != null)
			{
				((IDisposable)_KPIRepository).Dispose();
			}
			if(_KPIProgressRepository != null)
			{
				((IDisposable)_KPIProgressRepository).Dispose();
			}
		}

		private void BindData()
		{
			if(TheKPI != null)
			{
				this.lKPIContent.Text = TheKPI.KPIContent;

				var season = CommonHelper.GetSeason();

				if(TheKPI.TheKPIProgress != null)
				{
					if(season == "一季度")
					{
						lProgress.Text = TheKPI.TheKPIProgress.Quarter1Progress ?? string.Empty;
					}
					else if(season == "二季度")
					{
						lProgress.Text = TheKPI.TheKPIProgress.Quarter2Progress ?? string.Empty;
					}
					else if(season == "三季度")
					{
						lProgress.Text = TheKPI.TheKPIProgress.Quarter3Progress ?? string.Empty;
					}
					else if(season == "四季度")
					{
						lProgress.Text = TheKPI.TheKPIProgress.Quarter4Progress ?? string.Empty;
					}
				}

				if(TheKPI.TheKPIProgressForUpdate != null)
				{
					if(season == "一季度")
					{
						lProgressUpdated.Text = TheKPI.TheKPIProgressForUpdate.UpdatedQuarter1Progress ?? string.Empty;
					}
					else if(season == "二季度")
					{
						lProgressUpdated.Text = TheKPI.TheKPIProgressForUpdate.UpdatedQuarter2Progress ?? string.Empty;
					}
					else if(season == "三季度")
					{
						lProgressUpdated.Text = TheKPI.TheKPIProgressForUpdate.UpdatedQuarter3Progress ?? string.Empty;
					}
					else if(season == "四季度")
					{
						lProgressUpdated.Text = TheKPI.TheKPIProgressForUpdate.UpdatedQuarter4Progress ?? string.Empty;
					}
				}
			}
		}

		private bool CheckPermission()
		{
			var result = false;
			//var u = KPISiteContext.Current.CurrentUser;

			if(TheKPI != null && TheCurrentLoginUser != null)
			{
				if(TheCurrentLoginUser.RoleName.Equals("administrators") && (TheKPI.CurrentState == DataAccess.Models.KPIStates.Progress_Of_Modification_Modified_Review_Pending))
				{
					result = true;
				}
			}

			return result;
		}
	}
}